﻿
Partial Class Administracion_Perfiles
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Cargar los perfiles
        If Page.IsPostBack = False Then
            cagarPerfiles()
        End If
    End Sub
    Sub cagarPerfiles()
        Dim bd As New bdSeguridadDataContext
        Dim a = From p In bd.Web_Perfiles
                 Select p
        Me.GridView1.DataSource = a
        Me.GridView1.DataBind()
    End Sub

    Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing
        GridView1.EditIndex = e.NewEditIndex
        cagarPerfiles()
    End Sub

    Protected Sub GridView1_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles GridView1.RowCancelingEdit
        GridView1.EditIndex = -1
        cagarPerfiles()
    End Sub

    Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating

        Dim bd As New bdSeguridadDataContext
        Dim fila As GridViewRow
        fila = GridView1.Rows(GridView1.EditIndex)
        Dim llaves As DataKey = GridView1.DataKeys(GridView1.EditIndex)

        Dim CodPerfil As Integer = CInt(llaves.Item("CodPerfil"))
        Dim a = From p In bd.Web_Perfiles
                 Select p
                 Where p.CodPerfil = CodPerfil


        Dim pe As New Web_Perfile
        pe = a.FirstOrDefault
        pe.Perfil = CType(fila.FindControl("TextBox1"), TextBox).Text
        bd.SubmitChanges()
        bd.registrarLog(Context.User.Identity.Name, 2, Request.CurrentExecutionFilePath)
        GridView1.EditIndex = -1
        cagarPerfiles()

    End Sub

    Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting

        Dim bd As New bdSeguridadDataContext
        'Dim fila As GridViewRow
        'fila = GridView1.Rows(e.RowIndex)
        'Dim llaves As DataKey = GridView1.DataKeys(e.RowIndex)
        Dim CodPerfil As Integer = e.Keys("CodPerfil")
        Dim a = From p In bd.Web_Perfiles
                 Select p
                 Where p.CodPerfil = CodPerfil

        Dim pe As New Web_Perfile
        pe = a.FirstOrDefault
        Dim cuentaUsuarios = pe.Web_Usuarios.Count
        If cuentaUsuarios = 0 Then
            bd.Web_Perfiles.DeleteOnSubmit(pe)
            bd.SubmitChanges()
            bd.registrarLog(Context.User.Identity.Name, 2, Request.CurrentExecutionFilePath)
            cagarPerfiles()
        Else
            cutval.Enabled = True
            cutval.ErrorMessage = "El perfil tiene usuarios asociados, no se puede eliminar."
            cutval.IsValid = False
        End If
    End Sub

    Protected Sub btnCancelar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        txtPerfil.Text = ""
    End Sub

    Protected Sub btnGuardar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGuardar.Click
        Dim pe As New Web_Perfile
        pe.Perfil = txtPerfil.Text

        Using bd As New bdSeguridadDataContext
            bd.Web_Perfiles.InsertOnSubmit(pe)
            Try
                bd.SubmitChanges()
            Catch ex As Exception
                cutval.IsValid = False
                cutval.Enabled = True
                cutval.ErrorMessage = "No se puede crear el perfil"
            End Try
            bd.registrarLog(Context.User.Identity.Name, 2, Request.CurrentExecutionFilePath)
        End Using
        cagarPerfiles()
        txtPerfil.Text = ""
    End Sub
End Class
